package com.chen.utils.config;

import com.chen.utils.mapper.DataSourceInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Long
 * @date: 2025/8/7 21:46
 **/
@Slf4j
@Component
public class CreateTable {

    @Resource
    private DataSourceInfoMapper dataSourceInfoMapper;

    @PostConstruct
    public void createTab(){
        List<String> existsTable = dataSourceInfoMapper.selectTableList();
        Map<String, String> tableDdlSqlMap = tableDdlSqlMap();
        log.info("检查表是否存在: {}", tableDdlSqlMap.keySet());

        for (String tab : tableDdlSqlMap.keySet()) {
            long count = existsTable.stream().filter(t -> t.equals(tab)).count();
            if (count == 0){
                log.info("创建表: {}", tab);
                String ddlSql = tableDdlSqlMap.get(tab);
                dataSourceInfoMapper.createTable(ddlSql);
            }
        }
    }

    public Map<String, String> tableDdlSqlMap(){
        Map<String, String> map = new HashMap<>();
        // 数据源配置表
        map.put("data_source_info", "create table data_source_info( " +
                                    "    id text(255) not null, " +
                                    "    db_name text(1000), " +
                                    "    data_type text(255), " +
                                    "    url text(2000), " +
                                    "    library_name text(255), " +
                                    "    username text(255), " +
                                    "    password text(255), " +
                                    "    primary key (id) " +
                                    ");");

        // 自动填充配置表
        map.put("fill", "create table fill( " +
                        "    id text(255) not null, " +
                        "    column_name text(255), " +
                        "    field_fill_type text(255), " +
                        "    primary key (id) " +
                        ");");

        // 数据源 — 自动填充 关系表
        map.put("datasource_fill", "create table datasource_fill( " +
                "    id text(255) not null, " +
                "    datasource_id text(255), " +
                "    fill_id text(255), " +
                "    primary key (id) " +
                ");");

        // 策略配置表
        map.put("strategy_info", "create table strategy_info( " +
                                 "    id text(255) not null, " +
                                 "    name text(255), " +
                                 "    author text(255), " +
                                 "    parent_path text(255), " +
                                 "    ignore_table_prefixs text(2000), " +
                                 "    enable_swagger tinyint(1)  default 0, " +
                                 "    super_mapper_class text(500), " +
                                 "    logic_delete_column_name text(255), " +
                                 "    version_column_name text(255), " +
                                 "    fills_list text(2000), " +
                                 "    enable_lombok tinyint(1) default 0, " +
                                 "    super_service_impl_class text(500), " +
                                 "    entity_pack text(500), " +
                                 "    service_pack text(500), " +
                                 "    service_impl_pack text(500), " +
                                 "    mapper_pack text(500), " +
                                 "    ignore_boolean_prefix tinyint(1) default 0, " +
                                 "    primary key (id) " +
                                 ");");

        // 数据源 — 策略配置 关系表
        map.put("datasource_strategy", "create table datasource_strategy( " +
                "    id text(255) not null, " +
                "    datasource_id text(255), " +
                "    strategy_id text(255), " +
                "    primary key (id) " +
                ");");

        return map;
    }

}
